﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace dns批量处理
{
    public partial class login : Office2007Form
    {
        public login()
        {
            InitializeComponent();
            txtUser.Focus();
        }
        public bool isAuthorized = false;
        private void btnxLogin_Click(object sender, EventArgs e)
        {
            if (txtPasswd.Text == "" || txtUser.Text == "")
            {
                MessageBox.Show("请输入账户名或密码", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string url = " http://www.uuuwang.com/ologin";
            string postdata = "user=" + txtUser.Text.Trim() + "&passwd=" + txtPasswd.Text.Trim();
            try
            {
                if (PostHtmlString(url, postdata) == "1")
                {
                    isAuthorized = true;
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("错误的账户名或密码", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtPasswd.Focus();
                    return;
                }
            }
            catch (Exception ex)
            {

               MessageBox.Show(ex.Message);
                return;
            }

        }

        private string PostHtmlString(string URL, string postdata)
        {
            HttpWebRequest httpWebRequest;
            HttpWebResponse webResponse;

            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            httpWebRequest.Accept = "application/json, text/javascript, */*; q=0.01";
            httpWebRequest.Method = "Post";

            httpWebRequest.UserAgent =
                "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";

            byte[] byteRequest = Encoding.Default.GetBytes(postdata);
            //添加post数据
            httpWebRequest.ContentLength = byteRequest.Length;
            Stream stream;
            stream = httpWebRequest.GetRequestStream();
            stream.Write(byteRequest, 0, byteRequest.Length);
            stream.Close();

            webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            string strhtml = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8).ReadToEnd();
            // cookie = webResponse.Headers.Get("Set-Cookie");
            return strhtml;
        }

        private void btnxCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.uuuwang.com");
        }
    }
}
